<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>webSocket</title>
    <script src="socket.io.js"></script>
</head>
<body>
    <h1>Chat Room</h1>
    <input id="sendTxt" type="text"/>
    <button id="sendBtn">发送</button>
    <div id="recv"></div>
    <script>
        var socket =io("ws://100.120.7.68:3000/");
        function showMassage(data,type){
            var div =document.createElement('div')
            div.innerHTML=data;
            if(type=='enter'){
                div.style.color="blue"
            }else if(type=='leave'){
                div.style.color="red"
            }else if(type=="message"){
                div.style.color="black"
            }
            document.body.appendChild(div);
        }

        document.getElementById('sendBtn').onclick=function(){
          debugger
          var txt=document.getElementById('sendTxt').value;
          if(txt){
            socket.emit('message',txt);
          }
        }
        
        socket.on('enter',function(data){
          showMassage(data,'enter')
        })
        socket.on('leave',function(data){
          showMassage(data,'leave')
        })
        socket.on('message',function(data){
          showMassage(data,'message')
        })
       
    </script>
</body>
</html>